#include <NDL.h>
#include <SDL.h>
#include <string.h>
#include <assert.h>
#define keyname(k) #k,
uint8_t keystate[256] = {};
static const char *keyname[] = {
  "NONE",
  _KEYS(keyname)
};

int SDL_PushEvent(SDL_Event *ev) {
  return 0;
}

int SDL_PollEvent(SDL_Event *ev) {
  char buf[64];
  if(!NDL_PollEvent(buf, 64)){
    ev->key.keysym.sym = SDLK_NONE;
    ev->type = SDL_USEREVENT;
    return 0;
  }
  char key[15];
  char type[3];
  sscanf(buf, "%s %s\n", type, key);
  if(type[1] == 'd'){
    ev->type = SDL_KEYDOWN;
  }else{
    ev->type = SDL_KEYUP;
  }
  for(int i=0; i < 83; i++){
    if(strcmp(key,keyname[i]) == 0){
      ev->key.keysym.sym = i;
      keystate[i] = (type[1] == 'd') ? 1 : 0;
      break;
    }
  }
  return 1;
}

int SDL_WaitEvent(SDL_Event *event) {
  char buf[64];
  while(!NDL_PollEvent(buf, 64));
  char key[20];
  char type[3];
  sscanf(buf, "%s %s\n", type, key);
  if(type[1] == 'd'){
    event->type = SDL_KEYDOWN;
  }else{
    event->type = SDL_KEYUP;
  }

  for(int i=0; keyname[i] != NULL; i++){
    if(strcmp(key,keyname[i]) == 0){
      event->key.keysym.sym = i;
      keystate[i] = (type[1] == 'd') ? 1 : 0;
      break;
    }
  }
  
  return 1;
}

int SDL_PeepEvents(SDL_Event *ev, int numevents, int action, uint32_t mask) {
  return 0;
}

uint8_t* SDL_GetKeyState(int *numkeys) {
  if(numkeys == NULL)return keystate;
  else assert(0);
}
